Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Command line options' - 2 code snippet(s) found

 Sample 1. Reading / Parsing Command Line options using apache.commons.cli

public static void main(String[] args) {
   Option option1 = OptionBuilder.withArgName("option").hasArg().create("option");

   Options options = new Options();
   CommandLineParser parser = new GnuParser();
   options.addOption(option1);

   final CommandLine commandLine = parser.parse(options, args);
   final String[] commandLineArgs = commandLine.getArgs();

   if (commandLine.hasOption("option")) {
   }
}

   Like      Feedback     Parsing Command Line options  apache.commons.cli  CommandLineParser  GnuParser  OptionBuilder  Options


 Sample 2. Using kohsuke for command line options

Add the following dependency

<dependency>
      <groupId>org.kohsuke.args4j</groupId>
      <artifactId>args4j-maven-plugin</artifactId>
      <version>2.33</version>
</dependency>

In the main class add the following element

@Option(name = "-date", required = true, metaVar = "mmddyyyy", usage = "date for which we want this to happen")
private String date = null;

Within the main method add code to parse command line args and populate date

public static void main(String args[]){
try {
    parser.parseArgument(args);
    SimpleDateFormat commandLineDateFormat = new SimpleDateFormat("mmddyyyy", Locale.getDefault());
Date date = commandLineDateFormat.parse(date);

    } catch (Exception ex){
       ex.printStackTrace();
    }
}

   Like      Feedback     org.kohsuke.args4j.CmdLineParser  org.kohsuke.args4j.Option   kohsuke  command line options



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner